package com.magicode.portal.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;










import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.entiy.base.ChannelBuyHistory;
import com.magicode.cms.entiy.base.NewsBuyHistory;
import com.magicode.cms.entiy.base.OfflineEventsUserBuyHistory;
import com.magicode.cms.entiy.base.Video;
import com.magicode.cms.entiy.base.VipBuyHistory;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.common.util.DateUtil;
import com.magicode.portal.service.PermissionService;

@Service("portalpermissionService")
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	@Override
	public Boolean validVideoPermission(String userid, String refid) {
//		是否免费视频
		if(this.ifFreeVideo(refid)){
			return true;
		}
//		是否VIP用户
		if(this.validByVip(userid)){
			return true;
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userid", userid);
		map.put("newsid", refid);
//		是否为当前视频支付
		if(this.validByOrder(map)){
			return true;
		}
		return false;
	}
	/**
	 * 是否免费视频
	 * @param videoId
	 * @return
	 */
	private boolean ifFreeVideo(String videoId){
		boolean resultFlag = false;
		String videoSql = "select t.freeViewTime from "+Video.class.getName()+" t where t.id = :id ";
		Map paraMap = new HashMap();
		paraMap.put("id", videoId);
		Integer freeViewTime = null;
		List list = null;
		try {
			list = commonDaoService.queryList(videoSql, paraMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(list != null && list.size() > 0){
			freeViewTime = (Integer) list.get(0);
		}
		if(freeViewTime == null || freeViewTime == 0){
			resultFlag = true;
		}
		return resultFlag;
	}
	private String getValidByVipHql(String userid,boolean iscount )throws Exception{
		String hql_title="";
		if(iscount){
			hql_title = "select count(t.id)";
		}
		String hql =hql_title+" from "+VipBuyHistory.class.getName()+" t  where t.vipuser.id='"+userid+"' and t.validEndTime >'"
			    +DateUtil.getInstance().dateToString(new Date())+"' and t.status.code='Success' ";
		return hql;
	}
	
	private String getValidByVipHql(boolean iscount )throws Exception{
		String hql_title="";
		if(iscount){
			hql_title = "select count(t.id)";
		}
		String hql =hql_title+" from "+VipBuyHistory.class.getName()+" t  where t.vipuser.id=:id and t.validEndTime >'"
			    +DateUtil.getInstance().dateToString(new Date())+"' and t.status.code='Success' ";
		return hql;
	}
	
	private Boolean validByVip(String userid){
		try {
			long result =commonDaoService.countByHqlMap(getValidByVipHql(userid,true), null);
			System.out.println("vip--->"+result);
			return result>0?true:false;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}
	private String getValidByOrderHql(boolean iscount )throws Exception{
		String hql_title="";
		if(iscount){
			hql_title = "select count(t.id)";
		}
		String hql =" from "+NewsBuyHistory.class.getName()+" t  where t.user.id=:userid and t.news.id=:newsid and t.validEndTime >'"
			    +DateUtil.getInstance().dateToString(new Date())+"' and t.status.code='Success' ";
		return hql;
	}
	private Boolean validByOrder(Map<String,Object> map ){
		Boolean resultFlag = false;
		try {
			Long result =commonDaoService.countByHqlMap(getValidByOrderHql(true), map);
			System.out.println("order--->"+result);
			if( result != null && result >0){
				resultFlag = true;
			}
			else{
				
			}
			return resultFlag;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return resultFlag;
	}


	@Override
	public VipBuyHistory getVipByUserid(String userid) {
		try {
			String hql = this.getValidByVipHql(userid, false);
			String order = " order by t.validEndTime desc ";
			List <VipBuyHistory> vips = commonDaoService.queryList(hql+order, null);
			if(null!=vips&&vips.size()>0){
				return vips.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return null;
	}


	@Override
	public ChannelBuyHistory getChannelBuyHistoryByUserId(String userid,String refid) {
			try {
				Map<String,Object> map = new HashMap();
				map.put("userid", userid);
				map.put("channelId", refid);
				String hql = this.getValidByChannelBuyHistoryHql(false);
				String order = " order by t.validEndTime desc ";
				List <ChannelBuyHistory> channelBuyHistories = commonDaoService.queryList(hql+order, map);
				if(null!=channelBuyHistories&&channelBuyHistories.size()>0){
					return channelBuyHistories.get(0);
				}
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
			return null;
	}
	@Override
	public OfflineEventsUserBuyHistory getOfflineBuyHistoryByUserId(String userid, String objid) {
		try {
			Map<String,Object> map = new HashMap();
			map.put("userid", userid);
			map.put("offlineeventsuserid", objid);
			String hql = this.getValidByOfflineBuyHistoryHql(false);
			String order = " order by t.validEndTime desc ";
			List <OfflineEventsUserBuyHistory> offlineBuyHistories = commonDaoService.queryList(hql+order, map);
			if(null!=offlineBuyHistories&&offlineBuyHistories.size()>0){
				return offlineBuyHistories.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 是否存在待购买报告的记录
	 * @param iscount
	 * @return
	 * @throws Exception
	 */
	private String getValidByChannelBuyHistoryHql(boolean iscount) throws Exception {
		String hql_title="";
		if(iscount){
			hql_title = "select count(t.id)";
		}
		String hql =hql_title+" from "+ChannelBuyHistory.class.getName()+" t  where t.user.id=:userid and t.channel.id=:channelId and t.validEndTime >'"
			    +DateUtil.getInstance().dateToString(new Date())+"' and t.status.code='Waiting' ";
		return hql;
	}
	
	/**
	 * 是否存在待购买报告的记录
	 * @param iscount
	 * @return
	 * @throws Exception
	 */
	private String getValidByOfflineBuyHistoryHql(boolean iscount) throws Exception {
		String hql_title="";
		if(iscount){
			hql_title = "select count(t.id)";
		}
		String hql =hql_title+" from "+OfflineEventsUserBuyHistory.class.getName()+" t  left outer join fetch t.status where t.user.id=:userid and t.offlineEventsUser.id=:offlineeventsuserid and t.validEndTime >'"
			    +DateUtil.getInstance().dateToString(new Date())+"' and t.status.code='Waiting' ";
		return hql;
	}
	@Override
	public NewsBuyHistory getOrder(String userid, String refid) {
		try {
			Map<String,Object> map = new HashMap();
			map.put("userid", userid);
			map.put("newsid", refid);
			String hql = this.getValidByOrderHql(false);
			String order = " order by t.validEndTime desc ";
			List <NewsBuyHistory> news = commonDaoService.queryList(hql+order, map);
			if(null!=news&&news.size()>0){
				return news.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return null;
	}

}
